﻿using System;
using System.Data;
using System.Collections.Generic;
using ZHDJ.MobileInterface.Common;
using SXT.Party.Law.Model;

namespace ZHDJ.MobileInterface
{
    public partial class ToAddConsultation : AbstractBasePage
    {
        protected override OutParam BusinessProcess<InParam, OutParam>(InParam inParam)
        {
            // 业务逻辑返回的响应对象  
            AddConsultationOutParam result = new AddConsultationOutParam();
            AddConsultationInParam param = new AddConsultationInParam();
            string logMsg = "";
            bool isFlag = true;

            try
            {
                param = inParam as AddConsultationInParam;

                if (string.IsNullOrEmpty(param.Type))
                {
                    result.code = "0";
                    result.desc = "类型不能为空不能为空";

                    isFlag = false;
                }
                if (param.Type == "1" || param.Type == "2" || param.Type == "3" || param.Type == "4")
                {
                    if (param.Type == "1")
                    {
                        if (string.IsNullOrEmpty(param.LawyerID) || string.IsNullOrEmpty(param.Content) || string.IsNullOrEmpty(param.LoginID))
                        {
                            result.code = "0";
                            result.desc = "律师ID,咨询内容，咨询人ID不能为空不能为空";

                            isFlag = false;
                        }

                        if (isFlag)
                        {
                            PartyLawConsultation FA = new PartyLawConsultation();
                            FA.ConsultationID = StringHelper.NewID;
                            FA.Lawyer = param.LawyerID;
                            FA.Content = param.Content;
                            FA.ConsultationBy = param.LoginID;
                            FA.CreateTime = DateTime.Now;

                            result.desc = PartyLawConsultation.AddConsultation(FA, FA.ConsultationBy,"Mobile");
                        }
                    }
                    if (param.Type == "2")
                    {
                        if (string.IsNullOrEmpty(param.ConsultationID) || string.IsNullOrEmpty(param.RContent) || string.IsNullOrEmpty(param.ReplyBy) ||
                            string.IsNullOrEmpty(param.ReplyByType))
                        {
                            result.code = "0";
                            result.desc = "咨询ID，回复内容，回复人，回复类型不能为空";

                            isFlag = false;
                        }
                        if (param.ReplyByType == "1" && string.IsNullOrEmpty(param.LawyerID))
                        {
                            result.code = "0";
                            result.desc = "律师回复时，律师ID不能为空";

                            isFlag = false;
                        }
                        if (isFlag)
                        {
                            if (string.IsNullOrEmpty(param.ReplyID))
                            {
                                //添加追问/回复
                                PartyLawConsultationReply FA = new PartyLawConsultationReply();
                                FA.ReplyID = StringHelper.NewID;
                                FA.ReplyByType = Convert.ToInt32(param.ReplyByType);
                                FA.ReplyBy = param.ReplyBy;
                                FA.Content = param.RContent;
                                FA.CreateTime = DateTime.Now;
                                FA.ConsultationID = param.ConsultationID;

                                result.desc = PartyLawConsultationReply.AddConsultationReply(FA, param.LawyerID,"Mobile");
                            }
                            else
                            {
                                //编辑回复
                                PartyLawConsultationReply FA = new PartyLawConsultationReply();
                                FA.ReplyID = param.ReplyID;
                                FA.Load();
                                FA.Content = param.RContent;
                                if (ZHDJ.MobileInterface.Common.LawCs.UpdatePartyLawConsultationReply(param.RContent,param.ReplyID))
                                {
                                    result.desc = "su";
                                }
                                else result.desc = "fa";
                            }
                        }
                    }
                    if (param.Type == "3")
                    {
                        if (string.IsNullOrEmpty(param.ReplyID))
                        {
                            result.code = "0";
                            result.desc = "回复ID不能为空";

                            isFlag = false;
                        }
                        if (ZHDJ.MobileInterface.Common.LawCs.CheckReplyID(param.ReplyID))
                        {
                            result.code = "0";
                            result.desc = "回复或追问ID不存在，请确认";

                            isFlag = false;
                        }
                        if (isFlag)
                        {
                            result.desc = PartyLawConsultationReply.DelReply(param.ReplyID,"Mobile") == "delReply-su" ? "su" : "fa";
                        }
                    }
                    if (param.Type == "4")
                    {
                        if (string.IsNullOrEmpty(param.LawyerID) || string.IsNullOrEmpty(param.ConsultationID))
                        {
                            result.code = "0";
                            result.desc = "删除咨询时，律师ID和咨询ID不能为空，请确认";

                            isFlag = false;
                        }
                        if (isFlag)
                        {
                            if (ZHDJ.MobileInterface.Common.LawCs.CheckConsultationIDID(param.ConsultationID, param.LawyerID))
                            {
                                result.code = "0";
                                result.desc = "律师与咨询ID不匹配，请确认";

                                isFlag = false;
                            }
                        }
                        if (isFlag)
                        {
                            result.desc = PartyLawConsultation.DelConsultation(param.ConsultationID, param.LawyerID) == "delConsultation-su" ? "su" : "fa";
                        }

                    }
                }
                else { result.desc = "Type有误，请确认"; }
                if (result.desc == "su")
                {
                    result.desc = "操作成功";
                }
                logMsg = result.desc;
            }
            catch (Exception ex)
            {
                result.code = "0";
                result.desc = "转移过程出现异常，多次出现请联系管理员";
                logMsg = ex.Message + ":" + ex.StackTrace;
            }

            CommonUtils.InsertMobileInterfaceLog(SystemCode + "-ToAddConsultation", SerializeHelper.JsonSerializer<AddConsultationInParam>(param), logMsg);
            return result as OutParam;
        }

        protected void Page_Load(object sender, System.EventArgs e)
        {
            if (string.Compare(Request.QueryString["Safe"], "False", true) == 0)
                ProcessRequest<AddConsultationInParam, AddConsultationOutParam>();
            else
                SafeProcessRequest<AddConsultationInParam, AddConsultationOutParam>();
        }

    }

    #region 请求参数
    public class AddConsultationInParam
    {
        /// <summary>
        /// 类型 1：增加咨询 2：添加律师回复/编辑律师回复  3：律师删除回复 4：律师删除咨询
        /// </summary>
        public string Type { get; set; }

        /// <summary>
        /// 律师
        /// </summary>
        public string LawyerID { get; set; }

        /// <summary>
        /// 咨询人
        /// </summary>
        public string LoginID { get; set; }

        /// <summary>
        /// 内容
        /// </summary>
        public string Content { get; set; }




        /// <summary>
        /// 咨询ID
        /// </summary>
        public string ConsultationID { get; set; }

        /// <summary>
        /// 回复ID
        /// </summary>
        public string ReplyID { get; set; }

        /// <summary>
        /// 回复人
        /// </summary>
        public string ReplyBy { get; set; }

        /// <summary>
        /// 回复内容
        /// </summary>
        public string RContent { get; set; }

        /// <summary>
        /// 回复类型
        /// </summary>
        public string ReplyByType { get; set; }




    }
    #endregion

    #region 结果参数
    public class AddConsultationOutParam
    {
        public AddConsultationOutParam()
        {
            code = "1";
            desc = "操作成功";
        }

        /// <summary>
        /// 返回结果 具体见接口返回对照码 1	成功 0	失败 2	请求非法 加密结果和传入的MD5字符串进行比较，如果相同则执行接口操作，否则返回1001错误响应码
        /// </summary>
        public string code
        { get; set; }

        /// <summary>
        /// 结果描述 成功和失败都需要
        /// </summary>
        public string desc
        { get; set; }
    }
    #endregion
}